package data.nanjing.expo.services;

import data.nanjing.expo.models.dao.ActivityApplyInfoDao;
import data.nanjing.expo.models.dao.ActivityInfoDao;
import data.nanjing.expo.models.domain.ActivityApplyInfo;
import data.nanjing.expo.models.domain.dto.FavoriteShow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by zjp on 2018/6/28.
 * QQ：34948062
 * github: https://www.github.com/zjp1029
 * web： http://www.zjp1029.cn
 *
 * 活动报名
 */
@Service
public class ActivityApplyInfoService {

    @Autowired
    private ActivityApplyInfoDao activityApplyInfoDao;

    @Autowired
    private ActivityInfoDao activityInfoDao;

    //添加报名人数
    @Transactional
    public void addApplyNumber(ActivityApplyInfo activityApplyInfo) {
        activityApplyInfoDao.addActivityApplyInfo(activityApplyInfo);
        activityInfoDao.addActivityApplyNumber(activityApplyInfo.getActivityId());
    }

    //是否已经报名
    public boolean isApply(Integer userId, Integer activityId) {
        int count = activityApplyInfoDao.selectApplyCountByUserIdAndActivityId(userId, activityId);
        if (count == 0) {
            return false;
        }else {
            return true;
        }
    }

    //取消报名
    @Transactional
    public void deleteActivityApplyInfo(Integer userId, Integer activityId) {
        activityApplyInfoDao.deleteActivityApplyInfo(userId, activityId);
        activityInfoDao.increaseActivityApplyNumber(activityId);
    }

    //查询我的活动报名信息
    public List<FavoriteShow> selectApplyListByUserId(Integer userId, Integer pageNumber, Integer pageSize) {
        int pageStart = (pageNumber - 1) * pageSize;
        return activityApplyInfoDao.selectApplyListByUserId(userId, pageStart, pageSize);
    }

    //总数
    public Integer selectApplyCountByUserId(Integer userId) {
        return activityApplyInfoDao.selectApplyCountByUserId(userId);
    }

    //查询活动报名人
    public List<ActivityApplyInfo> selectApplyListByActivityId(Integer activityId, Integer pageNumber, Integer pageSize) {
        int pageStart = (pageNumber - 1) * pageSize;
        return activityApplyInfoDao.selectApplyListByActivityId(activityId, pageStart, pageSize);
    }

    public Integer selectApplyListCountByActivityId(Integer activityId) {
        return activityApplyInfoDao.selectApplyListCountByActivityId(activityId);
    }
}
